﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace SimSoDep
{
    public partial class frmTimKiemSoSim : Form
    {
       public  SqlConnection connect;
        SqlCommand m_SelectCommand;
        SqlDataAdapter DataAdapter;
        DataSet dataSet;
        DataTable datatable;
        public frmTimKiemSoSim()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn thoát ?", "Thong bao", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Close();
            }
        }

       public void ptbxViettel_Click(object sender, EventArgs e)
        {
            lblTB.Text = "VIETTEL";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("097");
            cbxDauSo.Items.Add("098");
            cbxDauSo.Items.Add("0165");
            cbxDauSo.Items.Add("0166");
            cbxDauSo.Items.Add("0167");
            cbxDauSo.Items.Add("0168");
            cbxDauSo.Items.Add("0169");
            cbxDauSo.SelectedIndex = 0;
            txtSoCuoi.Text = "";
            
            
        }

       public void lblDS_Click(object sender, EventArgs e)
        {

        }

       public void ptbxMobifone_Click(object sender, EventArgs e)
        {
            cbxDauSo.Items.Clear();
            lblTB.Text = "MOBIFONE";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("090");
            cbxDauSo.Items.Add("093");
            cbxDauSo.Items.Add("0120");
            cbxDauSo.Items.Add("0121");
            cbxDauSo.Items.Add("0122");
            cbxDauSo.Items.Add("0126");
            cbxDauSo.Items.Add("0128");
            cbxDauSo.SelectedIndex = 0;
        }

      public void ptbxVinafone_Click(object sender, EventArgs e)
        {
            cbxDauSo.Items.Clear();
            lblTB.Text = "VINAPHONE";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("091");
            cbxDauSo.Items.Add("094");
            cbxDauSo.Items.Add("0123");
            cbxDauSo.Items.Add("0125");
            cbxDauSo.Items.Add("0127");
            cbxDauSo.SelectedIndex = 0;
        }

        public void ptbxSfone_Click(object sender, EventArgs e)
        {
            cbxDauSo.Items.Clear();
            lblTB.Text = "S-FONE";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("095");
            cbxDauSo.SelectedIndex = 0;
        }

        public void ptbxVietNamMobile_Click(object sender, EventArgs e)
        {
            cbxDauSo.Items.Clear();
            lblTB.Text = "VIETNAMMOBILE";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("092");
            cbxDauSo.Items.Add("0188");
            cbxDauSo.SelectedIndex = 0;
        }

        public void ptbxBeeline_Click(object sender, EventArgs e)
        {
            cbxDauSo.Items.Clear();
            lblTB.Text = "BEELINE";
            cbxDauSo.Items.Add("None");
            cbxDauSo.Items.Add("0199");
            cbxDauSo.SelectedIndex = 0;
        }

        public void Connection(string ChuoiTruyVan)
        {
            string ChuoiKetNoi = "Server=localhost;Integrated security=SSPI;database=QUANLYSIMSODEP";
            connect = new SqlConnection(ChuoiKetNoi);

            try
            {
                connect.Open();
                m_SelectCommand = new SqlCommand(ChuoiTruyVan, connect);
                DataAdapter = new SqlDataAdapter(m_SelectCommand);
                dataSet = new DataSet();
                datatable = new DataTable();

                DataAdapter.Fill(dataSet);
                // Trích đối tượng DataTable từ DataSet.
                datatable = dataSet.Tables[0];
                // dataGridView
                dataGridView1.DataSource = datatable.DefaultView;

            }

            catch (Exception ex)
            {

                MessageBox.Show("Co loi trong qua trinh ket noi: " + ex.ToString());

            }
        }
        public void btnTim_Click(object sender, EventArgs e)
        {
            string ChuoiTruyVan;
            if (cbxDauSo.SelectedIndex != 0)
            {
                if (txtSoCuoi.Text != "")
                {
                    ChuoiTruyVan = "select * from SIMDEP where MASIM like '" + cbxDauSo.SelectedItem + "%' and MASIM like '%" + txtSoCuoi.Text + "'";
                    Connection(ChuoiTruyVan);
                }
                else if (cbxTamCo.Checked == true && cbxTuQuy.Checked == true)
                {
                    ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TAM CÔ' and LOAI = N'TỨ QUÝ' ";
                    Connection(ChuoiTruyVan);
                }

               else if (cbxTamCo.Checked == true)
                {
                    ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TAM CÔ' and MASIM like '" + cbxDauSo.SelectedItem + "%'";
                    Connection(ChuoiTruyVan);
                }
                else if (cbxTuQuy.Checked == true && cbxTamCo.Checked == false)
                {
                    ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TỨ QUÝ' and MASIM like '" + cbxDauSo.SelectedItem + "%'";
                    Connection(ChuoiTruyVan);
                }
                else
                {
                    ChuoiTruyVan = "select * from SIMDEP where MASIM like '" + cbxDauSo.SelectedItem + "%'";
                    Connection(ChuoiTruyVan);
                }
            }
            if (cbxDauSo.SelectedIndex == 0 && txtSoCuoi.Text == "")
            {
                if (lblTB.Text != "")
                {
                    ChuoiTruyVan = "select * from SIMDEP where NHAMANG = '" + lblTB.Text + "'";
                    Connection(ChuoiTruyVan);
                }
                
                else
                {
                    ChuoiTruyVan = "select * from SIMDEP";
                    Connection(ChuoiTruyVan);

                }

            }
           if (cbxTuQuy.Checked == true )
            {
                ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TỨ QUÝ' " ;
                Connection(ChuoiTruyVan);
            }
            if (cbxTamCo.Checked == true)
            {
                ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TAM CÔ' ";
                Connection(ChuoiTruyVan);
            }
            if (cbxTamCo.Checked == true && cbxTuQuy.Checked == true)
            {
                ChuoiTruyVan = "select * from SIMDEP where LOAI = N'TAM CÔ' or LOAI = N'TỨ QUÝ' ";
                Connection(ChuoiTruyVan);
            }
           
        }

       public void frmTimKiemSoSim_Load(object sender, EventArgs e)
        {
            string ChuoiTruyVan;
            ChuoiTruyVan = "select * from SIMDEP";
            Connection(ChuoiTruyVan);
        }

    }
}